<?php
namespace app\home\home;
use think\Db;
class Apireturn extends Base
{

    /**
     * 支付宝异步结果页面,请不要写具体的通知方法
     */
    public function notifyurl(){
        file_put_contents('returnurl.log','测试数据');
        $param = input('param.');
        $data['trade_no']=$param['trade_no'];
        $data['out_trade_no']=$param['out_trade_no'];
        $data['total_amount']=$param['total_amount'];
        $data['send_pay_date']=strtotime($param['gmt_payment']);//支付时间
        $this->updatadata($data);
    }


    /**
     * 微信异步结果页面,请不要写具体的通知方法
     */
    public function wxnotifyurl(){
        file_put_contents('wxreturnurl.log','测试数据');
        $param = $this->notify();
        if($param['result_code'] == 'SUCCESS'){
            $data['out_trade_no']=$param['out_trade_no'];
            $data['total_amount']=$param['total_fee']/100;
            $data['send_pay_date']=time();
            $data['trade_no']=$param['transaction_id'];
            $this->updatadata($data);
        }
    }


    private function updatadata($data){
        file_put_contents('ordernum.log',json_encode($data));
        $orderid = $data['out_trade_no'];
        if(strpos($orderid,'V') !== false){             //支付宝微信视频购买异步操作
            $result['pay_money']=$data['total_amount'];
            $result['status']=2;
            $result['paytime']=$data['send_pay_date'];
            $result['trade_no']=$data['trade_no'];
            Db::startTrans();
            $updatesave = db('home_orders')->where(['ordersn'=>$orderid])->update($result);//更改支付状态
            if($updatesave){
                $order = db('home_orders')->where(['ordersn'=>$orderid])->find();    //获取当前的订单信息
                //$usermoney=db('home_user')->where(['id'=>$order['vuid']])->setInc('money',$order['tearchprice']);//设置教师的余额
                $usermoney=1;
                if($usermoney){
                    $payresult = db('home_orders')->where(['ordersn'=>$orderid])->find();
                    if($payresult['paytype'] == 1){
                        echo "success";
                    }else{
                        echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
                    }
                    Db::commit();
                }else{
                    Db::rollback();
                    return "更新失败";
                }
            }else{
                //apistatus(0,'更新失败');
                return "更新失败";
            }
        }
        if(strpos($orderid,'T') !== false){       //支付宝微信账号充值
            $result['pay_price']=$data['total_amount'];
            $result['status']=2;
            $result['pay_time']=$data['send_pay_date'];
            $result['trade_no']=$data['trade_no'];
            Db::startTrans();
            $result = db('home_orders')->where(['ordersn'=>$orderid])->update($result);       //修改充值状态
            if($result){
                $uid = db('home_orders')->where(['ordersn'=>$orderid])->field('uid,chargeid,count')->find();     //获取当前的用户//获取当前的用户
                $updatesave = db('home_users')->where(['id'=>$uid['uid']])->setInc('rd_coin',$uid['count']);//添加用户的余额
                if($updatesave){
                    //apistatus(1,'更新成功');
                    $payresult = db('home_orders')->where(['ordersn'=>$orderid])->find();
                    file_put_contents('payresult.log',json_encode($payresult));
                    if($payresult['paytype'] == 1){
                        echo "success";
                    }else{
                        echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
                    }
                    Db::commit();
                }else{
                    Db::rollback();
                    //apistatus(0,'更新失败');
                    return "充值失败";
                }
            }
        }
    }



    /**
     * 成功结果返回页面
     */
    public function returnnotify(){
        $param = input('param.');
        $orderid = $param['out_trade_no'];
        if(strpos($orderid,"Z") !== false) {
            $vid = db('home_videoorder')->where(['orderid' => $orderid])->value('videoid');
            $this->assign('vid', $vid);
            return $this->fetch('paypage/returnnotify');
        }else{
            $recharge = db('home_recharge')->where(['orderid' => $orderid])->find();
            $this->assign('recharge', $recharge);
            return $this->fetch('paypage/returncnotify');
        }
    }

}